var twbOrderLogisticsVm = new Vue({
    el:'#twbOrderLogisticsApp',
    data:{
        page: {currentPage: 0, pageSizes: [20, 50, 100], pageSize: 20, total: 0, ascending: [], descending: []},
        twbOrderLogisticsData: [],
        totalAmount: null,
        twbFinStatus: [{
            value: '1',
            label: '未付款'
        }, {
            value: '2',
            label: '已付款'
        }],
        q:{
            orderNo: null,
            logisticsName: null,
            createTime: null,
            logiNo:null,
            beginLogiCost: null,
            endLogiCost: null,
            beginCreatetime: null,
            endCreatetime: null,
            finStatus: null,
        },
        logiPayFormVisible: false,
        logiPayForm: {
            logiPaytime: null,
            logiPayRemark: null,
            logiIds: []
        },
    },
    mounted: function(){
        this.query();
    },
    methods: {
        search: function () {
            this.page.currentPage = 0;
            this.query();
        },
        query: function () {
            var self = this;
            // 录入时间
            var createTime = self.q.createTime;
            if (!isBlank(createTime)){
                self.q.beginCreatetime = createTime[0] + ' 00:00:00';
                self.q.endCreatetime = createTime[1] + ' 23:59:59';
            }

            var queryParam = self.q;
            var page = self.page;
            queryParam.page = page.currentPage;
            queryParam.limit = page.pageSize;
            $.ajax({
                type: "POST",
                url: baseURL + "logistics/erplogistics/twbLogisticsList",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(queryParam),
                success: function (r) {
                    if (r.code == 0) {
                        self.totalAmount = r.userData.totalAmount;
                        self.page.total = r.page.totalCount;
                        self.twbOrderLogisticsData = r.page.list;
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 批量付款弹窗
        batchPaymentDialog: function () {
            var selected = this.$refs.twbOrderLogisticsTable.selection;
            if (selected.length == 0) {
                this.$message.error('请选择物流单号 !');
                return;
            }
            var notPayNum = 0;
            this.logiPayForm.logiIds = [];
            for (var s in selected){
                var item = selected[s];
                var logisticFinanceStatus = item.logisticFinanceStatus;
                if (logisticFinanceStatus != 2){
                    this.logiPayForm.logiIds.push(selected[s].logiId);
                    notPayNum += 1;
                }
            }
            if (notPayNum == 0){
                this.$message.error('请选择未付款的物流单号 !');
                return;
            }
            this.logiPayFormVisible = true;
        },
        // 批量付款
        batchPayment: function () {
            var self = this;
            var logiPaytime = self.logiPayForm.logiPaytime;
            if (isBlank(logiPaytime)){
                logiPaytime = new Date();
            }
            var twbLogisticsPaymentDto = {
                logiIds: self.logiPayForm.logiIds,
                logiPaytime: self.dateFormat(logiPaytime),
                logiPayRemark: self.logiPayForm.logiPayRemark,
            }
            $.ajax({
                type: "POST",
                url: baseURL + "logistics/erplogistics/twbLogisticsPayment",
                contentType: "application/json;charset=UTF-8",
                data: JSON.stringify(twbLogisticsPaymentDto),
                success: function (r) {
                    if (r.code == 0) {
                        self.logiPayFormVisible = false;
                        self.query();
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        // 页数变化
        handleSizeChange: function (val) {
            this.page.pageSize = val;
            this.query();
        },
        // 当前页变化
        handleCurrentChange: function (val) {
            this.page.currentPage = val;
            this.query();
        },
        reset:function(){
            this.q.orderNo = null;
            this.q.logisticsName = null;
            this.q.createTime = null;
            this.q.beginCreatetime = null;
            this.q.endCreatetime = null;
            this.q.logiNo = null;
            this.q.beginLogiCost = null;
            this.q.endLogiCost = null;
            this.q.finStatus = null;
        },
        dateFormat: function(date) {
            var year = date.getFullYear();
            var month = date.getMonth()+1<10 ? "0"+(date.getMonth()+1) : date.getMonth()+1;
            var day = date.getDate()<10 ? "0"+date.getDate() : date.getDate();
            var hours = date.getHours()<10 ? "0"+date.getHours() : date.getHours();
            var minutes = date.getMinutes()<10 ? "0"+date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds()<10 ? "0"+date.getSeconds() : date.getSeconds();
            return year+"-"+month+"-"+day+" "+hours+":"+minutes+":"+seconds;
        },
    }
});